package com.fengchunge.controller;

import com.fengchunge.common.ServerResponse;
import com.fengchunge.pojo.ServerInfo;
import com.fengchunge.pojo.SoftUpdateInformation;
import com.fengchunge.service.IEncounterAtticService;
import com.fengchunge.service.IFileService;
import com.fengchunge.util.PropertiesUtil;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author: tianmingtai
 * @create: 2021/04/26 21:37
 * @description: 逢春阁
 */

@Controller
@RequestMapping("/encounter/")
public class EncounterAtticController {

    @Autowired
    private IEncounterAtticService iEncounterAtticService;

    @Autowired
    private IFileService iFileService;


    /**
     * 统一上传接口
     *
     * @param file    文件
     * @param request 请求
     * @return URI和URL
     */
    @RequestMapping("upload.do")
    @ResponseBody
    public ServerResponse upload(@RequestParam(value = "file", required = false) MultipartFile file,
                                 HttpServletRequest request) {

        //上传文件业务逻辑
        String path = request.getSession().getServletContext().getRealPath("upload");
        String targetFileName = iFileService.upload(file, path);
        String url = PropertiesUtil.getProperty("ftp.server.http.prefix") + targetFileName;

        Map fileMap = Maps.newHashMap();
        fileMap.put("uri", targetFileName);
        fileMap.put("url", url);

        return ServerResponse.createBySuccess(fileMap);

    }


    /**
     * 添加一条信息
     *
     * @param serverInfo 实体类
     * @return 成功与否
     */
    @RequestMapping(value = "add_info.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> addInfo(ServerInfo serverInfo) {

        return iEncounterAtticService.addInfo(serverInfo);
    }


    /**
     * 编辑一条信息
     *
     * @param serverInfo 编辑信息的实体类
     * @return 成功与否
     */
    @RequestMapping(value = "edit_info.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> editInfo(ServerInfo serverInfo) {
        return iEncounterAtticService.editInfo(serverInfo);
    }


    /**
     * 审核信息
     *
     * @param id             信息id
     * @param approvalStatus 审核状态
     * @return 成功与否
     */
    @RequestMapping(value = "approval.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> approval(Long id, Integer approvalStatus) {

        // 以下情况也可以全局拦截
        Validate.isTrue(id != null, "信息id不能是空,,,,,");
        Validate.isTrue(approvalStatus != null, "审核状态approvalStatus不能是空");

        return iEncounterAtticService.approval(id, approvalStatus);
    }

    /**
     * 隐藏信息接口
     *
     * @param id           id
     * @param deleteStatus 隐藏状态
     * @return
     */
    @RequestMapping(value = "hidden.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> hidden(Long id, Integer deleteStatus) {

        return iEncounterAtticService.hidden(id, deleteStatus);
    }

    /**
     * 根据id 查询一条信息所有内容
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "getDetail.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<ServerInfo> getDetailById(Long id) {

        return iEncounterAtticService.getDetailById(id);
    }


    /**
     * 逻辑删除信息接口
     *
     * @param id
     * @param logicDelete
     * @return
     */
    @RequestMapping(value = "logic_delete.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> logicDelete(Long id, Integer logicDelete) {

        return iEncounterAtticService.logicDelete(id, logicDelete);
    }

    // 管理员获取列表 所有信息
    @RequestMapping(value = "manager_all_list.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<PageInfo> manageAllList(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                  @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                  String cityArea) {

        return iEncounterAtticService.manageAllList(pageNum, pageSize, cityArea);
    }

    /**
     * 普通用户获取列表 不隐藏、审核通过
     *
     * @param pageNum  页码
     * @param pageSize 页面大小
     * @return 具体数据
     */
    @RequestMapping(value = "general_user_list.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<PageInfo> generalUserList(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                    @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {

        return iEncounterAtticService.generalUserList(pageNum, pageSize);
    }

    /**
     * 通过地区和服务类型获取列表 (已删除要过滤)
     *
     * @param pageNum  页码
     * @param pageSize 页面大小
     * @return 具体数据
     */
    @RequestMapping(value = "get_list_by_cityArea_serviceType.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<PageInfo> getListByServiceTypeAndCityArea(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                                    @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                                    @RequestParam(value = "cityArea",defaultValue = "徐汇") String cityArea,
                                                                    @RequestParam(value = "serviceType", defaultValue = "3") Integer serviceType) {

        return iEncounterAtticService.getListByServiceTypeAndCityArea(pageNum, pageSize, cityArea, serviceType);
    }


    /**
     * 根据关键字查询 nickName（昵称），serverPrice（价格），addressInfo（地址信息），cityArea（地区）
     *
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "search_list.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<PageInfo> searchList(@RequestParam(value = "keyword", required = false) String keyword,
                                               @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                               @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {

        return iEncounterAtticService.searchList(keyword, pageNum, pageSize);
    }

    /**
     * 获取设备更新
     *
     * @return
     */
    @RequestMapping(value = "get_update_info.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<SoftUpdateInformation> getUpdateInfo() {

        return iEncounterAtticService.getUpdateInfo();
    }


}
